package yartiss.engine.simulation.system.taskset.generation;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import yartiss.engine.simulation.system.ITask;
import yartiss.engine.simulation.system.schedulable.SchedulableFactory;

public class FileTaskSetGenerator implements ITaskSetGenerator {

	private static int CURRENT_PRIORITY = 1;
	private List<ITask []> sets;
	private List<Double> utilizations;
	private int currentSet = 0;
	

	public FileTaskSetGenerator(InputStream file, long pr){
		this(file, null, pr);
	}

	public FileTaskSetGenerator(InputStream file, InputStream energyFile, long pr){
		loadTaskSets(file, energyFile);
	}
	
	@Override
	public String getName() {
		return "File tasksets generator";
	}

	@Override
	public String getDescription() {
		return "load sets from file and compute their energy cost from an energy file";
	}

	@Override
	public ITask[] generate() {
		ITask [] set = sets.get(currentSet++);
		currentSet %= sets.size();
		return set;
	}
	
	public int getSetsCount(){
		return sets.size();
	}


	private  void loadTaskSets(InputStream taskFileStream, InputStream energyFileStream) {
		sets = new ArrayList<ITask[]>();
		utilizations = new ArrayList<Double>();
		Scanner scanner  = new Scanner(taskFileStream);
		Scanner energyScanner  = energyFileStream != null ?  new Scanner(energyFileStream) : null;
		List<ITask> tasks = new ArrayList<ITask>();
		while(scanner.hasNext()){
			String line = scanner.nextLine();
			String [] params = line.split(" ");
			if(params.length==1) {
				if(!tasks.isEmpty()){
					ITask [] tab = new ITask [tasks.size()];
					sets.add(tasks.toArray(tab));
					CURRENT_PRIORITY = 1;
				}
				tasks.clear();
				utilizations.add(Double.parseDouble(params[0]));
			}
			else if(params.length > 1){
				long start=0,wcet=0,wcee=0,period=0,deadline=0;
				int priority = CURRENT_PRIORITY++ ;  

				for (int i = 0; i < params.length; i++) {
					String param = params[i].toLowerCase();
					
					if("period".equals(param)) 									period 		= Long.parseLong(params[i+2]);	
					else if("cost".equals(param) || "wcet".equals(param)) 		wcet 		= Long.parseLong(params[i+2]); 	
					else if("deadline".equals(param)) 							deadline	= Long.parseLong(params[i+2]);	
					else if("firstrelease".equals(param)||"start".equals(param)) start 		= Long.parseLong(params[i+2]); 	
					else if("wcee".equals(param)) 								wcee 		= Long.parseLong(params[i+2]);	
					else if("priorirty".equals(param)) 							priority  = Integer.parseInt(params[i+2]);
					
				}

				if(energyScanner != null)
					wcee = energyScanner.nextLong();
				wcee *= wcet;
				//				System.out.println(wcee+"\t%\t"+wcet+"\t= "+wcee%wcet);
				//				if(wcee/wcet <= pr){
				//			    	wcee = (pr+1)*wcet;
				//			    }
				//				long diff = wcee % wcet;
				//			    if(diff!=0)wcee-= diff;
				//				System.out.println(wcee+"\t%\t"+wcet+"\t= "+wcee%wcet);
				//				System.out.println("----------------------------------------------------");
				tasks.add(SchedulableFactory.newInstance("simple", start, wcet, wcee, period, deadline, priority));
			}
		}
		if(!tasks.isEmpty()){
			ITask [] tab = new ITask [tasks.size()];
			sets.add(tasks.toArray(tab));
		}
		scanner.close();
		if(energyScanner != null)
			energyScanner.close();
	}

	

}
